package com.bhs.sansonglogistics.ui.enterprise;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.DedicatedLineBean;
import com.bhs.sansonglogistics.bean.DedicatedLineDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.AbnormalOrderAdapter;
import com.bhs.sansonglogistics.ui.adapter.DedicatedLineAdapter;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 专线信息
 */
public class PrivateLineInformationFragment extends BaseFragment implements NetCallBack {


    private RecyclerView mRvList;
    private DedicatedLineAdapter mAdapter;
    private int page = 1;
    private int logistics_id;
    private SwipeRefreshLayout mRefreshLayout;


    public static PrivateLineInformationFragment newInstance(int logistics_id) {
        PrivateLineInformationFragment fragment = new PrivateLineInformationFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("logistics_id", logistics_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_private_line_information;
    }

    @Override
    public void initView(View root) {
        logistics_id = getArguments().getInt("logistics_id");
        mRvList = root.findViewById(R.id.rv_list);
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
    }

    @Override
    public void initData() {
        mAdapter = new DedicatedLineAdapter(R.layout.item_dedicated_line2);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        loadingPopupShow();
        loadData();
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DedicatedLineBean info = mAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.btn_range_transport:
                        startActivity(new Intent(getActivity(), RangeTransportActivity.class)
                                .putExtra("end_city", info.getEnd_city())
                                .putExtra("logistics_id", logistics_id)
                                .putExtra("end_county", info.getEnd_county())
                                .putExtra("end_province", info.getEnd_province()));
                        break;
                    case R.id.iv_status:
                    case R.id.tv_line_name:
                        info.setChecked(!info.isChecked());
                        mAdapter.notifyItemChanged(position);
                        break;
                    case R.id.fl_phone_number:
                        MyUtils.callPhone(getContext(), info.getLine_mobile());
                        break;
                    case R.id.fl_shipping_phone:
                        MyUtils.callPhone(getContext(), info.getOutlets_list().get(0).getOutlets_mobile());
                        break;
                }

            }
        });
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
    }

    private void loadData() {
        if (logistics_id > 0) {
            networkRequest(netApi.getSpecialList(page, 10, logistics_id), this);
        } else {
            networkRequest(netApi.getSpecialList(page, 10), this);
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        loadingPopupDismiss();
        DedicatedLineDataBean bean = new Gson().fromJson(json, DedicatedLineDataBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                bean.getData().getList().get(0).setChecked(true);
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}